package com.fingard.dsp.basicbankdata.ebank;

import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.FGBiz;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class EBankData {

    private static Map<String, String[]> _EBankMapping = new HashMap<String, String[]>();

    private static String MappingFilePath = StringHelper.trimEndFileSp(FGBiz.dspCenter.dspSet.rootDirPath + File.separator) + File.separator + "dsp-config/basicbankdata" + File.separator + "EBankMapping.xml";

    public static void refreshData() {
        _EBankMapping = null;
    }

    private static Object lockMutex = new Object();

    private static void init() {
        synchronized (lockMutex) {
            if (_EBankMapping.size() == 0) {
                try {
                    Map<String, String[]> eBankMapping = new HashMap<String, String[]>();
                    String strEBank = FileHelper.readFileText(MappingFilePath, "utf-8");
                    if (strEBank != null) {
                        XmlTextReader xmlReader = new XmlTextReader(strEBank);
                        while (xmlReader.read("/BankMapping/item")) {
                            XElement tmpItem = xmlReader.readedXElement;
                            String ats_bank_code = "";
                            String ats_bank_name = "";
                            String pbc_bank_code = "";
                            String pbc_bank_name = "";
                            String ats_area_name = "";

                            if (tmpItem.hashMapAttributes.containsKey("ats_bank_code")) {
                                ats_bank_code = tmpItem.hashMapAttributes.get("ats_bank_code");
                            }
                            if (tmpItem.hashMapAttributes.containsKey("ats_bank_name")) {
                                ats_bank_name = tmpItem.hashMapAttributes.get("ats_bank_name");
                            }
                            if (tmpItem.hashMapAttributes.containsKey("pbc_bank_code")) {
                                pbc_bank_code = tmpItem.hashMapAttributes.get("pbc_bank_code");
                            }
                            if (tmpItem.hashMapAttributes.containsKey("pbc_bank_name")) {
                                pbc_bank_name = tmpItem.hashMapAttributes.get("pbc_bank_name");
                            }
                            if (tmpItem.hashMapAttributes.containsKey("ats_area_name")) {
                                ats_area_name = tmpItem.hashMapAttributes.get("ats_area_name");
                            }

                            if (StringHelper.hasAnyChar(ats_area_name)) {
                                String tmpKeyBankAndArea = ats_bank_code + "-" + ats_area_name;
                                if (eBankMapping.containsKey(tmpKeyBankAndArea) == false) {
                                    eBankMapping.put(tmpKeyBankAndArea, new String[]{pbc_bank_code, pbc_bank_name});
                                }
                            } else {
                                if (StringHelper.hasAnyChar(ats_bank_code) && !eBankMapping.containsKey(ats_bank_code)) {
                                    eBankMapping.put(ats_bank_code, new String[]{pbc_bank_code, pbc_bank_name});
                                }
                                if (StringHelper.hasAnyChar(ats_bank_name) && !eBankMapping.containsKey(ats_bank_name)) {
                                    eBankMapping.put(ats_bank_name, new String[]{pbc_bank_code, pbc_bank_name});
                                }
                            }
                            if (eBankMapping.containsKey(pbc_bank_name) == false) {
                                eBankMapping.put(pbc_bank_name, new String[]{pbc_bank_code, pbc_bank_name});
                            }
                        }
                        _EBankMapping = eBankMapping;
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    public static String[] mappingEBankCode(String p_ATSBankCodeOrName, String p_ATSAreaName) throws Exception {
        String[] outEBankInfo = new String[]{"", ""};
        init();
        if (_EBankMapping.containsKey(p_ATSBankCodeOrName)) {
            outEBankInfo = _EBankMapping.get(p_ATSBankCodeOrName);
            return outEBankInfo;
        } else {
            String tmpKeyArea = p_ATSBankCodeOrName + "-" + p_ATSAreaName;
            if (_EBankMapping.containsKey(tmpKeyArea)) {
                outEBankInfo = _EBankMapping.get(tmpKeyArea);
                return outEBankInfo;
            }
        }
        return outEBankInfo;
    }


}
